import grace from "../../../utils/grace.js";
import {Address} from '/address-model.js';

let address = new Address();

grace.page({
    /**
     * 页面的初始数据
     */
    data: {
        name: "",
        mobile: "",
        detail: "",
        region: ['北京市', '北京市', '东城区'],
        customItem: '全部',
        checked: true,
        canSubmit: false
    },
    bindRegionChange(e) {
        this.$data.region = e.detail.value;
    },
    cancel() {
        this.$goBack();
    },
    formSubmit(e) {
        let params = {
            'token': wx.getStorageSync('token'),
            'parent_id': wx.getStorageSync('partner_id'),
            'name': e.detail.value.name,
            'mobile': e.detail.value.mobile,
            'state': this.$data.region[0],
            'city': this.$data.region[1],
            'street': this.$data.region[2].concat(e.detail.value.detail),
            'default_address': this.$data.checked
        };
        address.addAddressData(params, (data) => {
            if (data.code == 0) {
                wx.showToast({
                    title: '添加成功',
                    icon: 'success',
                })
            } else {
                wx.showToast({
                    title: '添加失败',
                    icon: 'loading',
                })
            }
            this.$goBack();
        })
    },
    checkboxChange() {
        if (this.$data.checked) {
            this.$data.checked = false;
        } else {
            this.$data.checked = true;
        }
    },
    changeName(e) {
        this.$data.name = e.detail.value;
        if (this.$data.name != "" && this.$data.mobile != "" && this.$data.detail != "") {
            this.$data.canSubmit = true;
        } else {
            this.$data.canSubmit = false;
        }
    },
    changeMobile(e) {
        this.$data.mobile = e.detail.value;
        if (this.$data.name != "" && this.$data.mobile != "" && this.$data.detail != "") {
            this.$data.canSubmit = true;
        } else {
            this.$data.canSubmit = false;
        }
    },
    changeDetail(e) {
        this.$data.detail = e.detail.value;
        if (this.$data.name != "" && this.$data.mobile != "" && this.$data.detail != "") {
            this.$data.canSubmit = true;
        } else {
            this.$data.canSubmit = false;
        }
    }
})